package com.lyf.scm.admin.domain.service.impl;

import cn.hutool.core.date.DateUtil;
import com.lyf.scm.admin.api.dto.*;
import com.lyf.scm.admin.domain.remote.allocationservice.facade.SalePlanDirectlyDifferinFacade;
import com.lyf.scm.admin.domain.remote.item.facade.ItemCoreFacade;
import com.lyf.scm.admin.domain.service.SalePlanDirectlyDifferinService;
import com.lyf.scm.admin.domain.util.BaseSearchUtil;
import com.lyf.scm.common.enums.DataDisplayCycleEnum;
import com.lyf.scm.common.enums.ExcelExportTypeEnum;
import com.lyf.scm.common.enums.ResponseMsg;
import com.lyf.scm.common.model.ExcelExportDTO;
import com.lyf.scm.common.model.SalesPlanSelectDTO;
import com.lyf.scm.common.util.date.MyDateUtil;
import com.rome.arch.core.exception.RomeException;
import com.rome.user.common.utils.UserContext;
import feign.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 直营预测计划_目标与销售计划差异
 *
 * @author wangchlei
 * @date 2020/08/05
 */
@Slf4j
@Service
public class SalePlanDirectlyDifferinServiceImpl implements SalePlanDirectlyDifferinService {
    @Resource
    private SalePlanDirectlyDifferinFacade salePlanDirectlyDifferinFacade;

    @Resource
    private ItemCoreFacade itemCoreFacade;

    /**
     * 按条件查询直营预测计划_目标与销售计划差异(周)
     *
     * @param salePlanDirectlyDifferinDTO
     * @return {@link PlanResponseDTO<SalePlanDirectlyDifferinInfoDTO>}
     **/
    @Override
    public PlanResponseExpandDTO<SalePlanDirectlyDifferinInfoDTO> listWeekBySelective(SalePlanDirectlyDifferinDTO salePlanDirectlyDifferinDTO) {
        //获得最低级品类查询条件
        List<SalesPlanSelectDTO> categorySalesPlanSelectDTOs = itemCoreFacade.getSubcategoryCode(salePlanDirectlyDifferinDTO.getCategoryCodeList());
        //赋值品类搜索条件
        BaseSearchUtil.setSearchFields(salePlanDirectlyDifferinDTO, null, categorySalesPlanSelectDTOs, null);
        //根据起始时间获取14周的日期等数据（如果Gmv指标存在 还会赋值Gmv相关信息）
        List<Map<String, Date>> weekDates = MyDateUtil.getWeekDates(salePlanDirectlyDifferinDTO.getStartTime(), DataDisplayCycleEnum.WEEKLY_FOURTEEN.getValue());
        salePlanDirectlyDifferinDTO.setWeekDateList(weekDates);
        //设置结束日期，方便sql查询命中索引
        salePlanDirectlyDifferinDTO.setEndTime(MyDateUtil.getLastDate(null, weekDates));
        salePlanDirectlyDifferinDTO.setCreator(UserContext.getUserId());
        //数据展示周期
        PageInfoExpandDTO<SalePlanDirectlyDifferinInfoDTO> pageInfo = salePlanDirectlyDifferinFacade.listWeekBySelective(salePlanDirectlyDifferinDTO);
        PlanResponseExpandDTO<SalePlanDirectlyDifferinInfoDTO> planResponse = new PlanResponseExpandDTO();
        //格式化日期格式
        planResponse.setValidGmv(pageInfo.isValidGmv());
        planResponse.setInfoList(pageInfo.getList());
        planResponse.setDateList(MyDateUtil.processCyclesDate(weekDates, "M.dd"));
        planResponse.setTotal(pageInfo.getTotal());
        return planResponse;
    }

    /**
     * 图表数据接口
     *
     * @param salePlanDirectlyDifferinDTO
     * @return {@link PlanResponseDTO<SalePlanDirectlyDifferinInfoDTO>}
     **/
    @Override
    public EChartsExpandDTO eChartsData(SalePlanDirectlyDifferinDTO salePlanDirectlyDifferinDTO) {
        Date startTime = salePlanDirectlyDifferinDTO.getStartTime();
        Date endTime = salePlanDirectlyDifferinDTO.getEndTime();
        List<Map<String, Date>> weekDates = null;
        salePlanDirectlyDifferinDTO.setStartTime(DateUtil.beginOfDay(DateUtil.beginOfWeek(startTime)));
        //按周
        if (null != endTime) {
            //结束时间设置为周日
            salePlanDirectlyDifferinDTO.setEndTime(DateUtil.beginOfDay(DateUtil.endOfWeek(endTime)));
            weekDates = MyDateUtil.getWeekDates(startTime, endTime);
        }
        if (null == endTime) {
            //默认14周
            weekDates = MyDateUtil.getWeekDates(startTime, DataDisplayCycleEnum.WEEKLY_FOURTEEN.getValue());
            salePlanDirectlyDifferinDTO.setEndTime(MyDateUtil.getLastDate(null, weekDates));
        }
        salePlanDirectlyDifferinDTO.setWeekDateList(weekDates);
        salePlanDirectlyDifferinDTO.setCreator(UserContext.getUserId());
        return salePlanDirectlyDifferinFacade.listAllByCycles(salePlanDirectlyDifferinDTO);
    }

    /**
     * 导出
     *
     * @param excelExportDTO
     * @param type
     * @return {@link Response}
     **/
    @Override
    public Response export(ExcelExportDTO excelExportDTO, Integer type) {
        if (type == null) {
            throw new RomeException(ResponseMsg.FAIL_2005.getCode(), ResponseMsg.FAIL_2005.getMsg());
        }
        SalePlanDirectlyDifferinDTO salePlanDirectlyDifferinDTO = new SalePlanDirectlyDifferinDTO();
        salePlanDirectlyDifferinDTO.setAreaCodeList(excelExportDTO.getAreaCodeList());
        salePlanDirectlyDifferinDTO.setLayeredCodeList(excelExportDTO.getLayeredCodeList());
        salePlanDirectlyDifferinDTO.setUnitType(excelExportDTO.getUnitType());
        salePlanDirectlyDifferinDTO.setCategoryCodeList(excelExportDTO.getCategoryCodeList());
        salePlanDirectlyDifferinDTO.setStartTime(excelExportDTO.getStartTime());
        //根据起始时间获取14周的日期数据
        List<Map<String, Date>> weekDates = MyDateUtil.getWeekDates(excelExportDTO.getStartTime(), DataDisplayCycleEnum.WEEKLY_FOURTEEN.getValue());
        salePlanDirectlyDifferinDTO.setWeekDateList(weekDates);
        //设置结束日期，方便sql查询命中索引
        salePlanDirectlyDifferinDTO.setEndTime(MyDateUtil.getLastDate(null, weekDates));
        //导出当页数据
        if (ExcelExportTypeEnum.CURRENT_PAGE_EXPORT.getType().equals(type)) {
            salePlanDirectlyDifferinDTO.setPageNum(excelExportDTO.getPageNum());
            salePlanDirectlyDifferinDTO.setPageSize(excelExportDTO.getPageSize());
        }
        //导出勾选
        if (ExcelExportTypeEnum.CHECKED_BOX_EXPORT.getType().equals(type)) {
            salePlanDirectlyDifferinDTO.setPageNum(excelExportDTO.getPageNum());
            salePlanDirectlyDifferinDTO.setPageSize(excelExportDTO.getPageSize());
            salePlanDirectlyDifferinDTO.setRowIndex(excelExportDTO.getRowIndex());
        }
        //导出全部数据
        //获取三个查询条件的实际集合
        List<SalesPlanSelectDTO> categorySalesPlanSelectDTOs = itemCoreFacade.getSubcategoryCode(excelExportDTO.getCategoryCodeList());
        BaseSearchUtil.setSearchFields(salePlanDirectlyDifferinDTO, null, categorySalesPlanSelectDTOs, null);
        salePlanDirectlyDifferinDTO.setCreator(UserContext.getUserId());
        return salePlanDirectlyDifferinFacade.export(salePlanDirectlyDifferinDTO, type);
    }
}
